package com.csyf.manager.dao;

import com.csyf.manager.entity.SysTradeSummary;
import org.apache.commons.lang.StringUtils;

/**
 * @Author: Enzo
 * @Date: 2018/6/27 10:18
 */
public class CurDataSqlProvider {


    public String selectCurdata(SysTradeSummary vo) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" SELECT \n" +
                "  agent_id agentId,agent_name agentName,DATE_FORMAT(create_time,'%Y%m%d') tradeTime,SUM(CASE WHEN trade_status = 'TRADE_SUCCESS' THEN profit ELSE 0 END) profit,service_type serviceType,mch_id mchId,\n" +
                "  COUNT(1) totalCount,\n" +
                "  SUM(\n" +
                "    CASE\n" +
                "      WHEN trade_status = 'TRADE_SUCCESS' \n" +
                "      THEN total_fee \n" +
                "      ELSE 0\n" +
                "    END\n" +
                "  ) successMoney,\n" +
                "  SUM(\n" +
                "    CASE\n" +
                "      WHEN trade_status <> 'TRADE_SUCCESS' \n" +
                "      THEN total_fee \n" +
                "      ELSE 0\n" +
                "    END\n" +
                "  ) failMoney,\n" +
                "  COUNT(\n" +
                "    CASE\n" +
                "      WHEN trade_status = 'TRADE_SUCCESS' \n" +
                "      THEN 1 \n" +
                "      ELSE NULL\n" +
                "    END\n" +
                "  ) successCount,\n" +
                "  COUNT(\n" +
                "    CASE\n" +
                "      WHEN trade_status <> 'TRADE_SUCCESS' \n" +
                "      THEN 1 \n" +
                "      ELSE NULL\n" +
                "    END\n" +
                "  ) failCount \n" +
                "FROM\n" +
                "  `sys_trade`  WHERE DATE_FORMAT(create_time,'%Y%m%d') = DATE_FORMAT(#{tradeTime},'%Y%m%d') \n");

        if (StringUtils.isNotBlank(vo.getMchId())) {
            stringBuffer.append("AND mch_id =").append("'"+ vo.getMchId() + "'");
        }

        if (StringUtils.isNotBlank(vo.getServiceType())) {
            stringBuffer.append("AND service_type in (").append("'"+vo.getServiceType()+"'").append(")");
        }


        stringBuffer.append("GROUP BY agent_id ,agent_name,mch_id,service_type,DATE_FORMAT(create_time,'%Y%m%d')");
        return stringBuffer.toString();
    }
}
